//
//  BYProductListController.m
//  OCDemo
//
//  Created by zhumuzhi on 2022/2/13.
//  商品列表

#import "BYProductListController.h"
#import "BYProductListCell.h"
#import "BYProductDetailsController.h"
#import "ProductModel.h"

static NSString *const cellIdentifierID = @"BYProductListCellID";

@interface BYProductListController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation BYProductListController

#pragma mark - SetData

- (void)setDataList:(NSArray *)dataList {
    _dataList = dataList;
    
    [self.collectionView reloadData];
}

#pragma mark - LifeCycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.collectionView];   
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView*)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView*)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataList.count;
}

- (__kindof UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath
{
    BYProductListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifierID forIndexPath:indexPath];
    cell.model = self.dataList[indexPath.row];
    return cell;
}

#pragma mark - UICollectionViewDelegateFlowLayout

- (CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath*)indexPath
{
    return CGSizeMake(kScreenWidth, [BYProductListCell CellHeight]);
}

#pragma mark - UICollectionViewDelegate

- (void)collectionView:(UICollectionView*)collectionView didSelectItemAtIndexPath:(NSIndexPath*)indexPath
{
    ProductModel *model = self.dataList[indexPath.row];
    BYProductDetailsController *controlelr = [[BYProductDetailsController alloc] init];
    controlelr.detailsProductId = model.product_id;
    [self.navigationController pushViewController:controlelr animated:YES];
}

#pragma mark - LazyGet
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        flowLayout.minimumLineSpacing  = 0.0f;
        flowLayout.minimumInteritemSpacing = 0.0f;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-kSafeAreaTopHeight-kSafeAreaBottomHeight) collectionViewLayout:flowLayout];
        _collectionView.dataSource=self;
        _collectionView.delegate=self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.backgroundColor = [UIColor clearColor];
        [_collectionView registerClass:[BYProductListCell class] forCellWithReuseIdentifier:cellIdentifierID];
    }
    return _collectionView;
}

@end
